package cloud.shyidong.admin.commerce.service;

import cloud.shyidong.commerce.dao.CommerceCategoryDao;
import cloud.shyidong.commerce.entity.CommerceCategoryEntity;
import cloud.shyidong.commerce.request.CommerceCategoryInsertRequest;
import cloud.shyidong.commerce.request.CommerceCategoryPageRequest;
import cloud.shyidong.commerce.request.CommerceCategoryUpdateRequest;
import cloud.shyidong.core.exception.BusinessException;
import cloud.shyidong.core.request.LongIdRequest;
import cloud.shyidong.core.service.AbstractService;
import cloud.shyidong.core.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 电商商品分类业务处理
 * </p>
 *
 * @author syd
 * @since 2023-09-18 15:34:23
 */
@Service
public class CommerceCategoryService extends AbstractService<CommerceCategoryDao, CommerceCategoryEntity, LongIdRequest, CommerceCategoryInsertRequest, CommerceCategoryUpdateRequest, CommerceCategoryPageRequest> {
    
    @Override
    public void beforeUpdate(CommerceCategoryUpdateRequest updateRequest) {
        
        CommerceCategoryEntity commerceCategoryEntity = getDao().selectById(updateRequest.getId());
        if (ObjectUtils.isNull(commerceCategoryEntity)) {
            throw BusinessException.from("未查找到商品分类");
        }
        
        QueryWrapper<CommerceCategoryEntity> commerceCategoryEntityQueryWrapper = new QueryWrapper<>();
        commerceCategoryEntityQueryWrapper.eq("tenant_id", commerceCategoryEntity.getTenantId());
        commerceCategoryEntityQueryWrapper.and(queryWrapper -> queryWrapper.eq("name", updateRequest.getName()).or().eq("display_name", updateRequest.getDisplayName()));
        commerceCategoryEntityQueryWrapper.ne("id", updateRequest.getId());
        if (getDao().selectCount(commerceCategoryEntityQueryWrapper) > 0) {
            throw BusinessException.from("商品分类名称或商品分类显示名称已经重复");
        }
        
    }
    
}
